package com.ch.nettyplus.server.channel;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * @author CH
 * @date 2022/2/24
 */
@Slf4j
@ChannelHandler.Sharable
public class StringChannelHandler extends SimpleChannelInboundHandler<String> {
    private int readIdleTimes = 0;
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg){
        System.out.println(msg);
    }
    /**
     * 心跳处理
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt){
        IdleStateEvent event = (IdleStateEvent) evt;
        switch (event.state()) {
            case READER_IDLE:
                log.info("服务器当前状态，读空闲");
                readIdleTimes++;
                break;
            case WRITER_IDLE:
                log.info("服务器当前状态，写空闲");
                // 不处理
                break;
            case ALL_IDLE:
                log.info("服务器当前状态，读写空闲");
                // 不处理
                break;
            default:
                log.info("服务器当前状态，其他");
        }
        log.info("超时次数：{}", readIdleTimes);
    }
}
